package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.ReturnMethodEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.LendMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.mapper.LendItemMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.InvestVO;
import com.atguigu.srb.core.service.LendItemService;
import com.atguigu.srb.core.util.Amount1Helper;
import com.atguigu.srb.core.util.LendNoUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-13
 */
@Service
public class LendItemServiceImpl extends ServiceImpl<LendItemMapper, LendItem> implements LendItemService {


    @Resource
    LendMapper lendMapper;

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public BigDecimal getInterestCount(BigDecimal investAmount, Long lendId, Long userId) {

        //初始化 interestCount
        BigDecimal interestCount = new BigDecimal("0");
        //获得还款方式
        Lend lend = lendMapper.selectById(lendId);
        Integer returnMethod = lend.getReturnMethod();

        //获得年利率
        BigDecimal lendYearRate = lend.getLendYearRate();
        //获得还款期数
        Integer period = lend.getPeriod();

        //计算收益 interestCount = 年化利率 * 期数 * 总金额 * 还款方式
        //等额本息
        if (returnMethod== ReturnMethodEnum.ONE.getMethod()){
            interestCount = Amount1Helper.getInterestCount(investAmount,lendYearRate,period);
        }
        //等额本金
        if (returnMethod== ReturnMethodEnum.TWO.getMethod()){
            interestCount = Amount1Helper.getInterestCount(investAmount,lendYearRate,period);
        }
        //按期付款
        if (returnMethod== ReturnMethodEnum.THREE.getMethod()){
            interestCount = Amount1Helper.getInterestCount(investAmount,lendYearRate,period);
        }
        //一次性
        if (returnMethod== ReturnMethodEnum.FOUR.getMethod()){
            interestCount = Amount1Helper.getInterestCount(investAmount,lendYearRate,period);
        }
        return interestCount;
    }

    @Override
    public String commitInvest(InvestVO investVO, Long investUserId) {
        //获得投资人的信息
        UserInfo investUserInfo = userInfoMapper.selectById(investUserId);

        //获得借款人的信息
        Long lendId = investVO.getLendId();
        Lend lend = lendMapper.selectById(lendId);
        Long borrowerUserId = lend.getUserId();
        UserInfo borrowerUserInfo = userInfoMapper.selectById(borrowerUserId);

        //将投标信息存入lendItem表
        LendItem lendItem = new LendItem();
        //生成这条交易的流水号
        String lendItemNo = LendNoUtils.getLendItemNo();
        lendItem.setLendItemNo(lendItemNo);
        //投资人的investUserId
        lendItem.setInvestUserId(investUserId);//投资人用户id
        lendItem.setLendId(investVO.getLendId()); //标的id
        lendItem.setInvestName(investUserInfo.getName());//投资人名字
        lendItem.setLendEndDate(lend.getLendEndDate());//利息结束时间
        lendItem.setInvestAmount(new BigDecimal(investVO.getInvestAmount()));//投资金额
        lendItem.setLendStartDate(lend.getLendStartDate());//利息开始时间
        lendItem.setLendYearRate(lend.getLendYearRate());//年化利率
        lendItem.setStatus(0); //状态
        //预期收益
        BigDecimal interestCount
                = getInterestCount(new BigDecimal(investVO.getInvestAmount()), lend.getId(), investUserId);
        lendItem.setExpectAmount(interestCount);
        //实际收益
        lendItem.setRealAmount(new BigDecimal("0"));
        baseMapper.insert(lendItem);


        //生成投标汇付宝表单
        Map<String,Object> map = new HashMap<>();
        map.put("agentId",HfbConst.AGENT_ID);
        map.put("voteBindCode",investUserInfo.getBindCode());//投资人的绑定码
        map.put("benefitBindCode",borrowerUserInfo.getBindCode());//借款人的绑定码
        map.put("agentProjectCode",lend.getLendNo());//标的单号
        map.put("agentProjectName",lend.getTitle());//标的名称
        map.put("agentBillNo",lendItemNo);//投标生成的单号
        map.put("voteAmt",investVO.getInvestAmount());//投资金额
        map.put("votePrizeAmt",new BigDecimal("0"));
        map.put("voteFeeAmt",new BigDecimal("0"));
        map.put("projectAmt",lend.getAmount());//标的总金额
        map.put("note",null);//描述
        map.put("notifyUrl",HfbConst.INVEST_NOTIFY_URL);
        map.put("returnUrl",HfbConst.INVEST_RETURN_URL);
        map.put("timestamp", RequestHelper.getTimestamp());
        map.put("sign",RequestHelper.getSign(map));

        String form = FormHelper.buildForm(HfbConst.INVEST_URL, map);

        return form;
    }
}
